using System;
using System.Data;
using System.Xml;
using vs.Core;
using vs.Authentication;
namespace Forum
{
    public partial class member_list : System.Web.UI.UserControl
    {
        protected vs.Paging.vsPaging paging;

        private XmlDocument MemberListXML(int pg)
        {
            int page_size = CommonFunction.PageSize;
            int start = pg * page_size;
            string strSQL = "SELECT     u.UserName, u.Fullname, u.Email, u.RegistedDate, m.Posts, m.Slogan, m.ImageUrl "
                + " FROM USR_Users u LEFT OUTER JOIN FORUM_Member m ON u.UserName = m.UserName ORDER BY RegistedDate DESC";
            DataTable dt = DataFactory.LoadPage(strSQL, start, page_size);

            XmlDocument doc = new XmlDocument();
            XmlElement root = CommonFunction.LoadRootNode(doc, "<memberlist></memberlist>");

            XmlElement row = doc.CreateElement("row");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string imageurl = dt.Rows[i]["ImageUrl"].ToString();
                if (imageurl == "") imageurl = UrlProvider.RootFolder + "/images/noimage.gif";
                DateTime regDate = Convert.ToDateTime(dt.Rows[i]["RegistedDate"]);
                string link = UrlProvider.AdminUrl("fr_member", "info", dt.Rows[i]["UserName"].ToString());
                XmlElement xmlmember = doc.CreateElement("member");

                CommonFunction.AddNode(doc, xmlmember, "UserName", dt.Rows[i]["UserName"].ToString());
                CommonFunction.AddNode(doc, xmlmember, "FullName", dt.Rows[i]["FullName"].ToString());
                CommonFunction.AddNode(doc, xmlmember, "Email", dt.Rows[i]["Email"].ToString());
                CommonFunction.AddNode(doc, xmlmember, "RegistedDate", regDate.ToString("yyyy-MM-dd hh:mm"));
                CommonFunction.AddNode(doc, xmlmember, "Posts", dt.Rows[i]["Posts"].ToString());
                CommonFunction.AddNode(doc, xmlmember, "Slogan", dt.Rows[i]["Slogan"].ToString());
                CommonFunction.AddNode(doc, xmlmember, "ImageUrl", imageurl);
                CommonFunction.AddNode(doc, xmlmember, "link", link);

                if ((Authenticate.IsAdmin || Authenticate.IsStaff))
                {
                    string link_edit = UrlProvider.AdminUrl("fr_member", "edit", dt.Rows[i]["UserName"].ToString());
                    CommonFunction.AddNode(doc, xmlmember, "link_edit", link_edit);
                }
                row.AppendChild(xmlmember);

                if ((i + 1) % 2 == 0)
                {
                    root.AppendChild(row);
                    row = doc.CreateElement("row");
                }
            }
            root.AppendChild(row);

            return doc;

        }

        private int CountPage()
        {
            
            int count = UserDB.CountUser();
            int page_size = CommonFunction.PageSize;
            int pg = count / page_size;
            if (page_size * pg < count) pg++;

            string membercount = "<b>@@count<b> members";
            lblMemberCount.Text = membercount.Replace("@@count", count.ToString());
            return pg;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!paging.IsPostBack)
                {
                    int pg = 0;
                    if (Request.QueryString["ID"] != null)
                        pg = Convert.ToInt32(Request.QueryString["ID"]) - 1;
                    int page = CountPage();
                    if (page > 1)
                    {
                        paging.Visible = true;
                        string format = UrlProvider.AdminUrl("fr_member", "list", "@@page");
                        paging.showing(page, pg, format);
                    }

                    XmlDocument doc = MemberListXML(pg);
                    string skin = ModSettingController.GetModuleSetting("skin", Utils.ModuleID);
                    string template = engine_xml.get_template_folder(skin) + "memberlist.xsl";
                    CommonFunction.XMLTransform(xmlMemberList, template, doc);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}